<%@page import="fi.foyt.cs.persistence.domainmodel.oauth.AuthorizedClient"%>
<%@page import="fi.foyt.cs.controller.OAuthController"%>
<%@page import="fi.foyt.cs.api.SystemProperty"%>
<%@page import="fi.foyt.cs.Settings"%>
<%@page import="fi.foyt.cs.controller.SystemController"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<%
  OAuthController oAuthController = new OAuthController();  
  String clientId = Settings.getValue(SystemProperty.ADMIN_CLIENT);
  AuthorizedClient authorizedClient = oAuthController.findAuthorizedClientByClientId(clientId);
  if (authorizedClient != null) {
    pageContext.setAttribute("adminClientId", authorizedClient.getClientId());
    pageContext.setAttribute("adminClientSecret", authorizedClient.getClientSecret());
    pageContext.setAttribute("adminReturnUrl", authorizedClient.getReturnUrl());   
    pageContext.setAttribute("adminToken", session.getAttribute("adminOAuth"));
  }
%>
<script type="text/javascript" src="${pageContext.request.contextPath}/script/criminalsouls-api/criminalsouls-api.js"></script>
    <script type="text/javascript">
  var _CRIMINALSOULSAPI = null;
  
  function getCriminalSoulsApi() {
	  if (!_CRIMINALSOULSAPI) {
		  _CRIMINALSOULSAPI = new CriminalSoulsApi('${adminClientId}', '${adminClientSecret}', '${adminReturnUrl}', '${adminToken}');
	  }
	  
	  return _CRIMINALSOULSAPI;
  }
  
  if (!getCriminalSoulsApi().isAuthorized()) {
	  getCriminalSoulsApi().authorize();
  }
</script>